/*
**  Copyright (c) 2014 Michael Liu(michael.liu.point@gmail.com).
**
**  Project: Gateway Unified Management Platform
**  File:    samgr.h
**  Author:  Michael
**  Date:    03/20/2014
**
**  Purpose:
**    system admin management defines.
*/

#ifndef __SYSADMMGR_H__
#define __SYSADMMGR_H__

#ifdef __cplusplus
extern "C" {
#endif				/* __cplusplus */

/* Include files. */

#include "common.h"
#include "cmrc.h"
#include "list.h"
#include "sadev.h"

/* Macro constant definitions. */

#define SYS_ADMIN_PASSWORD_SIZE			(32)
#define COMMON_FILE_NAME_SIZE			(128)

#define CFG_MGR_CFG_FILE_NAME_SIZE		COMMON_FILE_NAME_SIZE
#define FW_FILE_NAME_SIZE				COMMON_FILE_NAME_SIZE

/* Type definitions. */

	enum {
		eSA_SUB_MOD_SYS_MGR,
		eSA_SUB_MOD_CFG_MGR,
		eSA_SUB_MOD_FW_MGR,
		eSA_SUB_MOD_LOG_MGR,
	} esa_sub_module;

	enum {
		eSYS_MGR_LANG_CHINESE_SIMPLE,
		eSYS_MGR_LANG_CHINESE_TRADITIONAL,
		eSYS_MGR_LANG_ENGLISH,
		/* ToDo: Add all support language. */
	} esys_mgr_lang;

	enum {
		eSYS_MGR_OPT_VERIFY_PASSWORD,	/* Check password. */
		eSYS_MGR_OPT_SET_PASSWORD,	/* Set password. */
		eSYS_MGR_OPT_GET_PASSWORD,	/* Get password. */
		eSYS_MGR_OPT_SET_QIS,	/* Set Quick Install Setup Status. */
		eSYS_MGR_OPT_GET_QIS,	/* Get Quick Install Setup Status. */
		eSYS_MGR_OPT_GET_DATETIME,	/* Get date & time. */
		eSYS_MGR_OPT_SET_DATETIME,	/* Set date & time or set NTP server. */
		eSYS_MGR_OPT_GET_LANGUAGE,	/* Get Language. */
		eSYS_MGR_OPT_SET_LANGUAGE,	/* Set Language. */
		eSYS_MGR_OPT_GET_TIMEZONE,	/* Get Timezone. */
		eSYS_MGR_OPT_SET_TIMEZONE,	/* Set Timezone. */
		eSYS_MGR_OPT_GET_POWER_SAVE,	/* Get Timezone. */
		eSYS_MGR_OPT_SET_POWER_SAVE,	/* Set Timezone. */
		eSYS_MGR_OPT_GET_UPTIME,	/* Get system uptime. */
		eSYS_MGR_OPT_REBOOT,	/* Reboot. */
	} esys_mgr_opt;

	enum {
		eCFG_MGR_OPT_DUMP_CFG,	/* Dump config to file. */
		eCFG_MGR_OPT_LOAD_CFG,	/* Load config from file. */
		eCFG_MGR_OPT_RESET_CFG,	/* Reset config. */
	} ecfg_mgr_opt;

	enum {
		eFW_MGR_OPT_GET_VERSION,	/* Get current version. */
		eFW_MGR_OPT_DO_LOCAL_UPGRADE,	/* Upload fireware and upgrade. */
		eFW_MGR_OPT_START_FOTA_CHECK,
		eFW_MGR_OPT_QUERY_FOTA_CHECK,
		eFW_MGR_OPT_CANCEL_FOTA_CHECK,
		eFW_MGR_OPT_START_FOTA_UPGRADE,
		eFW_MGR_OPT_QUERY_FOTA_UPGRADE,
		eFW_MGR_OPT_CANCEL_FOTA_UPGRADE,
	} efw_mgr_opt;

	enum {
		eLOG_MGR_DUMP_LOG,	/* Dump log to file. */
	} elog_mgr_opt;

	enum {
		eSYS_TZ_OPT_UTC12,
		eSYS_TZ_OPT_UTC11,
		eSYS_TZ_OPT_UTC10,
		eSYS_TZ_OPT_NAST9DST,
		eSYS_TZ_OPT_PST8DST,
		eSYS_TZ_OPT_MST7DST_1,
		eSYS_TZ_OPT_MST7_2,
		eSYS_TZ_OPT_MST7DST_3,
		eSYS_TZ_OPT_CST6DST_1,
		eSYS_TZ_OPT_CST6DST_2,
		eSYS_TZ_OPT_CST6DST_3,
		eSYS_TZ_OPT_CST6DST_3_1,
		eSYS_TZ_OPT_UTC6,
		eSYS_TZ_OPT_EST5DST,
		eSYS_TZ_OPT_UTC5_1,
		eSYS_TZ_OPT_UTC5_2,
		eSYS_TZ_OPT_AST4DST,
		eSYS_TZ_OPT_UTC4_1,
		eSYS_TZ_OPT_UTC4DST_2,
		eSYS_TZ_OPT_NST3_30DST,
		eSYS_T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S_TZ_OPT_CCT_8,
		eSYS_TZ_OPT_WAS_8DST,
		eSYS_TZ_OPT_UTC_8DST,
		eSYS_TZ_OPT_UTC_9_1,
		eSYS_TZ_OPT_JST_9,
		eSYS_TZ_OPT_CST_9_30DST,
		eSYS_TZ_OPT_UTC_9_30,
		eSYS_TZ_OPT_UTC_10_3,
		eSYS_TZ_OPT_UTC_10_1,
		eSYS_TZ_OPT_UTC_10_2,
		eSYS_TZ_OPT_TST_10TDT,
		eSYS_TZ_OPT_UTC_10_5,
		eSYS_TZ_OPT_UTC_11_2,
		eSYS_TZ_OPT_UTC_11,
		eSYS_TZ_OPT_UTC_11_1,
		eSYS_TZ_OPT_UTC_12,
		eSYS_TZ_OPT_NZST_12DST,
		eSYS_TZ_OPT_UTC_13,
	} esys_tz_opt;

	typedef struct {
		unsigned char verify_ok;	/* eBOOL_TURE or eBOOL_FALSE */
		char password[SYS_ADMIN_PASSWORD_SIZE];
	} tpswd_body;

	typedef struct {
		unsigned char qis_done;	/* eBOOL_TURE or eBOOL_FALSE */
	} tget_qis_body;

	typedef struct date_time tget_uptime_rsp_body;
	typedef struct date_time tget_datetime_rsp_body;

	typedef struct {
		unsigned int tz;	/* esys_tz_opt */
		unsigned char use_ntp;	/* eBOOL_TURE or eBOOL_FALSE */
		struct date_time datetime;	/* use_ntp == eBOOL_FALSE */
		char ntp_server[SYS_ADMIN_NTP_SERVER_URL_SIZE];	/* use_ntp == eBOOL_TRUE */
	} tdatetime_body;

	typedef struct sys_mgr_opt_body tcm_sa_sm_req_body;
	typedef struct sys_mgr_opt_body tcm_sa_sm_rsp_body;
	struct sys_mgr_opt_body {
		unsigned char opt;	/* esys_mgr_opt */
		union {
			tpswd_body pswd;
			tget_qis_body qis;
			tdatetime_body datetime;
			tget_uptime_rsp_body uptime;
			unsigned char lang;	/* esys_mgr_lang */
			unsigned char power_save;	/* eBOOL_TRUE or eBOOL_FALSE */
		};
	};

	typedef struct cfg_mgr_opt_body tcm_sa_cm_req_body;
	typedef struct cfg_mgr_opt_body tcm_sa_cm_rsp_body;
	struct cfg_mgr_opt_body {
		unsigned char opt;	/* ecfg_mgr_opt */
		char cfg_file[CFG_MGR_CFG_FILE_NAME_SIZE];	/* indicate dump or load file name. */
	};

	typedef struct fw_mgr_opt_body tcm_sa_fm_req_body;
	typedef struct fw_mgr_opt_body tcm_sa_fm_rsp_body;
	struct fw_mgr_opt_body {
		unsigned char opt;	/* efw_mgr_opt */
		union {
			tversion fm_info;	/* for eFW_MGR_OPT_GET_VERSION and
						   eFW_MGR_OPT_DO_FOTA_CHECK */
			char fw_file[FW_FILE_NAME_SIZE];	/* for eFW_MGR_OPT_DO_LOCAL_UPGRADE */
		};
	};

	typedef struct log_mgr_opt_body tcm_sa_lm_req_body;
	typedef struct log_mgr_opt_body tcm_sa_lm_rsp_body;
	struct log_mgr_opt_body {
		unsigned char opt;	/* elog_mgr_opt */
		union {
			char log_file[FW_FILE_NAME_SIZE];	/* for eLOG_MGR_DUMP_LOG */
		};
	};

	typedef struct cm_sa_opt_body tcm_sa_req_body;
	typedef struct cm_sa_opt_body tcm_sa_rsp_body;
	struct cm_sa_opt_body {
		unsigned char submod;	/* indicate req_body type:esa_sub_module */
		unsigned char body[0];	/* . */
	};

/* External function declarations. */

	extern e_ret sys_admin_init(tsys_admin * sa);
	extern e_ret sys_admin_start(void);
	extern e_ret sys_admin_proc_req(tcm_req_body * req, tcm_rsp_body ** rsp);

/* Macro API definitions. */

#define sa_led_opt(led_dev, opt_method) \
do {																		\
	tsa_dev *led = (led_dev);												\
	if (led) {																\
		led->opt(led, opt_method, NULL);									\
	} else {																\
		sysmgr_dbg("NULL led device\n");									\
	}																		\
} while (0)

/* Global variable declarations. */

#ifdef __cplusplus
}				/* extern "C" */
#endif				/* __cplusplus */
#endif				/* __SYSADMMGR_H__ */
